package br.usp.wta.validator;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import br.usp.language.morph.AnalisadorMorfologico;
import br.usp.language.morph.MorphologicAnalyser;
import br.usp.language.syntax.SyntacticAnalyser;
import br.usp.language.syntax.grammar.ContextFreeGrammar;

public class IvoneValidator implements GrammarValidator {
    
    private static final String DICFILE = "resources/port";
    private static final String LISTFILE = "resources/port_amor_config.list";

    private ContextFreeGrammar grammar;
    private List<String> positiveStataments, negativeStatements;

    private MorphologicAnalyser morphologicAnalyser;
    private SyntacticAnalyser syntacticAnalyser;
    
    private double correctAcceptions, correctRejections, wrongAcceptions, wrongRejections;
    
    public IvoneValidator(String grammarFile) throws IOException {

        this.grammar = GrammarProvider.getGrammar(grammarFile);
        this.positiveStataments = StatementsProvider.getPositiveStatements();
        this.negativeStatements = StatementsProvider.getNegativeStatements();
        
        morphologicAnalyser = new AnalisadorMorfologico(DICFILE);
        this.syntacticAnalyser = new SyntacticAnalyser(morphologicAnalyser, grammar);
    }

    public void verify() {
        
        for (String sentence: this.positiveStataments) {
            
            StringReader reader = new StringReader(sentence);
            this.morphologicAnalyser.setInput(reader);
            if (this.syntacticAnalyser.analyse2())
                this.correctAcceptions++;
            else
                this.wrongAcceptions++;
            this.syntacticAnalyser.reset();
        }

        for (String sentence: this.negativeStatements) {
            
            StringReader reader = new StringReader(sentence);
            this.morphologicAnalyser.setInput(reader);
            if (!this.syntacticAnalyser.analyse2())
                this.correctRejections++;
            else
                this.wrongRejections++;
            this.syntacticAnalyser.reset();
        }

    }
    
    public double getCorrectAcceptionRate() {
        
        return this.correctAcceptions / (this.correctAcceptions + this.wrongAcceptions);
    }

    public double getCorrectRejectionRate() {
        
        return this.correctRejections / (this.correctRejections + this.wrongRejections);
    }
    
    public void printResult() {
        
        System.out.println("Correct accpetion rate: " + this.getCorrectAcceptionRate());
        System.out.println("Correct rejection rate: " + this.getCorrectRejectionRate());
    }

}
